﻿Imports System.Text.RegularExpressions
Imports System.Configuration
Public Class frmNha
    Dim appcofig As AppSettingsReader = New AppSettingsReader
    Public username As String = "BoCau"
    Dim ngaymacdinh As String = ""
    Private Sub TrangChủToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrangChủToolStripMenuItem.Click
        Me.Close()
    End Sub
    Public Function ThuPhi() As Single
        Return Single.Parse(txtGia.Text) * 50 / 100
    End Function
    Private Sub frmNha_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim loainha As DataTable = LoaiNhaThueBUS.GetAllLoaiNhaThueBUS()
        Dim tinhtrang As DataTable = LoaiTinhTrangBUS.GetAllLoaiTinhTrangBUS()
        Dim quan As DataTable = QuanBUS.GetAllQuanBUS()
        Dim congty As DataTable = CongTyBUS.GetAllCongTyBUS()

        cbLoaiNha.DataSource = loainha
        cbLoaiNha.DisplayMember = "TenLoaiNha"
        cbLoaiNha.ValueMember = "MaLoaiNha"

        cbTinhTrang.DataSource = tinhtrang
        cbTinhTrang.DisplayMember = "TenTinhTrang"
        cbTinhTrang.ValueMember = "MaTinhTrang"

        cbQuan.DataSource = quan
        cbQuan.DisplayMember = "TenQuan"
        cbQuan.ValueMember = "MaQuan"

        cbCongTy.DataSource = congty
        cbCongTy.DisplayMember = "TenCongTy"
        cbCongTy.ValueMember = "MaCongTy"

        cbLoaiNhaTimKiem.DataSource = loainha
        cbLoaiNhaTimKiem.DisplayMember = "TenLoaiNha"
        cbLoaiNhaTimKiem.ValueMember = "MaLoaiNha"

        cbQuanTimKiem.DataSource = quan
        cbQuanTimKiem.DisplayMember = "TenQuan"
        cbQuanTimKiem.ValueMember = "MaQuan"



        dgvNha.DataSource = NhaThueBUS.GetAllNhaBUS()


        ngaymacdinh = AppSettingsWriter.GetValue("NgayTiepNhanNhaThue")

        If (ngaymacdinh = 1) Then
            Dim d As Date = New Date(Date.Now.Year, Date.Now.Month, Date.Now.Day + 1)
            dpNgayTiepNhan.Text = d.ToString()
        End If



    End Sub
    Private Sub dgvNha_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvNha.SelectionChanged
        If (dgvNha.SelectedRows.Count > 0 And dgvNha.SelectedRows.Count < 2) Then

            txtMaNha.Text = dgvNha.SelectedRows(0).Cells("MaNha").Value.ToString()
            txtMoTa.Text = dgvNha.SelectedRows(0).Cells(1).Value.ToString()
            cbLoaiNha.SelectedValue = dgvNha.SelectedRows(0).Cells(2).Value.ToString()
            txtDienTich.Text = dgvNha.SelectedRows(0).Cells(3).Value.ToString()
            txtGia.Text = dgvNha.SelectedRows(0).Cells(4).Value.ToString()

            Dim cog As String = dgvNha.SelectedRows(0).Cells(5).Value.ToString()
            If (cog = "c") Then
                cbCanNguoiOGhep.Checked = True
            Else
                cbCanNguoiOGhep.Checked = False
            End If

            txtNguoiLienHe.Text = dgvNha.SelectedRows(0).Cells(6).Value.ToString()
            Dim dc As String = dgvNha.SelectedRows(0).Cells(7).Value.ToString()
            Dim chuoi() As String = dc.Split("/")

            txtSoNha.Text = chuoi(0)
            txtDuong.Text = chuoi(1)
            cbQuan.SelectedText = chuoi(2)


            txtDienThoai.Text = dgvNha.SelectedRows(0).Cells(8).Value.ToString()
            txtEmail.Text = dgvNha.SelectedRows(0).Cells(9).Value.ToString()
            dpNgayTiepNhan.Text = dgvNha.SelectedRows(0).Cells(10).Value.ToString()
            cbTinhTrang.SelectedValue = dgvNha.SelectedRows(0).Cells(11).Value.ToString()
            cbCongTy.SelectedValue = dgvNha.SelectedRows(0).Cells(12).Value.ToString()
            ' txtNguoiLienHe.Text = dgvNha.SelectedRows(0).Cells(13).Value.ToString()


            txtPCongTy.Text = cbCongTy.Text
            txtpMaNha.Text = txtMaNha.Text
            txtPhi.Text = ThuPhi().ToString()
        End If

    End Sub
    Private Sub ThêmToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThêmToolStripMenuItem.Click

        Dim match As String = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

        Dim reg As Regex = New Regex(match)
        Dim t As Long = 0
        Dim nha As NhaThueDTO = New NhaThueDTO()
        If (txtDienTich.Text = "" Or txtGia.Text = "" Or txtSoNha.Text = "" Or txtDuong.Text = "" Or (Long.TryParse(txtDienThoai.Text, t) = False) Or txtDienThoai.TextLength > 10 Or txtDienThoai.TextLength < 7 Or (reg.IsMatch(txtEmail.Text) = False And txtEmail.Text <> "")) Then
            MessageBox.Show("Diện tích, số nhà,giá, đường không được để trống.Số điện thoại, email có thể không đúng")
        Else
            Try




                nha.MoTa = txtMoTa.Text
                nha.MaLoaiNha = Integer.Parse(cbLoaiNha.SelectedValue.ToString())
                nha.DienTich = Single.Parse(txtDienTich.Text)
                nha.Gia = Single.Parse(txtGia.Text)
                If cbCanNguoiOGhep.Checked = True Then
                    nha.CanOGhep = "c"
                Else
                    nha.CanOGhep = "k"
                End If
                nha.NguoiLienHe = txtNguoiLienHe.Text
                nha.DiaChi = txtSoNha.Text & "/" & txtDuong.Text & "/" + cbQuan.Text
                nha.SoDienThoai = txtDienThoai.Text

                nha.Email = txtEmail.Text

                nha.NgayTiepNhan = dpNgayTiepNhan.Value.Date
                nha.MaTinhTrang = Integer.Parse(cbTinhTrang.SelectedValue.ToString())
                nha.MaCongTy = Integer.Parse(cbCongTy.SelectedValue.ToString())
                nha.NguoiDangTin = username
                NhaThueBUS.InsertNhaBUS(nha)
                dgvNha.DataSource = NhaThueBUS.GetAllNhaBUS()

                txtPCongTy.Text = cbCongTy.Text
                Dim dtma As DataTable = NhaThueBUS.GetMaNhaBUS()
                txtpMaNha.Text = dtma.Rows(dtma.Rows.Count() - 1)(0)
                txtPhi.Text = ThuPhi().ToString()

            Catch ex As Exception
                MessageBox.Show("Thêm không thành công, bạn vui lòng kiểm tra lại cơ sở dữ liệu và kết nối")
            End Try
        End If


    End Sub

    Private Sub XóaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XóaToolStripMenuItem.Click
        If (dgvNha.SelectedRows.Count > 0) Then
            Dim MaNha As Integer = Integer.Parse(dgvNha.SelectedRows(0).Cells(0).Value.ToString())
            NhaThueBUS.Delete_CapNhat_TinhTrang_NhaBUS(MaNha)
            dgvNha.DataSource = NhaThueBUS.GetAllNhaBUS()
        Else
            MessageBox.Show("Bạn chưa chọn dòng để xóa")
        End If
    End Sub

    Private Sub CậpNhậtToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CậpNhậtToolStripMenuItem.Click
        Dim match As String = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

        Dim reg As Regex = New Regex(match)

        Dim t As Long = 0

        If (txtDienTich.Text = "" Or txtGia.Text = "" Or txtSoNha.Text = "" Or txtDuong.Text = "" Or Long.TryParse(txtDienThoai.Text, t) = False Or txtDienThoai.TextLength > 10 Or txtDienThoai.TextLength < 7 Or (reg.IsMatch(txtEmail.Text) = False And txtEmail.Text <> "")) Then
            MessageBox.Show("Diện tích, số nhà,giá, đường không được để trống.Số điện thoại, email có thể không đúng")
        Else
            Try

                Dim nha As NhaThueDTO = New NhaThueDTO()
                nha.MaNha = Integer.Parse(txtMaNha.Text)
                nha.MoTa = txtMoTa.Text
                nha.MaLoaiNha = Integer.Parse(cbLoaiNha.SelectedValue.ToString())
                nha.DienTich = Single.Parse(txtDienTich.Text)
                nha.Gia = Single.Parse(txtGia.Text)
                If cbCanNguoiOGhep.Checked = True Then
                    nha.CanOGhep = "c"
                Else
                    nha.CanOGhep = "k"
                End If
                nha.NguoiLienHe = txtNguoiLienHe.Text
                nha.DiaChi = txtSoNha.Text & "/" & txtDuong.Text & "/" + cbQuan.Text
                nha.SoDienThoai = txtDienThoai.Text
                nha.Email = txtEmail.Text
                nha.NgayTiepNhan = dpNgayTiepNhan.Value.Date
                nha.MaTinhTrang = Integer.Parse(cbTinhTrang.SelectedValue.ToString())
                nha.MaCongTy = Integer.Parse(cbCongTy.SelectedValue.ToString())
                nha.NguoiDangTin = username
                NhaThueBUS.UpdateNhaBUS(nha)
                dgvNha.DataSource = NhaThueBUS.GetAllNhaBUS()


                txtPCongTy.Text = cbCongTy.Text
                'Dim dtma As DataTable = NhaThueBUS.GetMaNhaBUS()
                txtpMaNha.Text = txtMaNha.Text
                txtPhi.Text = ThuPhi().ToString()

            Catch ex As Exception
                MessageBox.Show("Sửa không thành công, bạn vui lòng kiểm tra lại cơ sở dữ liệu và kết nối")
            End Try
        End If

    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click

        Try
            Dim tencongty As String = txtTenCongTy.Text
            Dim maloainha As Integer = Integer.Parse(cbLoaiNhaTimKiem.SelectedValue.ToString())
            Dim tenquan As String = cbQuanTimKiem.Text.ToString()
            Dim loai As Integer = cbTimKiem.SelectedIndex
            dgvNha.DataSource = NhaThueBUS.TimKiemNhaBUS(tencongty, maloainha, tenquan, loai)
        Catch ex As Exception
            MessageBox.Show("Tìm kiếm bị lỗi")
        End Try



    End Sub

    Private Sub cbTimKiem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbTimKiem.SelectedIndexChanged


    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        dgvNha.DataSource = NhaThueBUS.GetAllNhaBUS()
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        frmImportNhaTuFileExcel.ShowDialog()
        dgvNha.DataSource = NhaThueBUS.GetAllNhaBUS()
    End Sub
End Class